﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Study
{
    public partial class 对DataTable进行条件筛选 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt= DBAccess.ExecuteDataTable("select * from TB_Condition");

            DataTable dt2 = SreeenDataTable(dt, "ConID<10 or (ConID>20 and ConID<=30)","ConID desc");
        }

        public DataTable SreeenDataTable(DataTable dt, string strWhere,string sort)
        {
            if (dt.Rows.Count <= 0) return dt;        //当数据为空时返回
            DataTable dtNew = dt.Clone();         //复制数据源的表结构
            DataRow[] dr = null;
            if (string.IsNullOrEmpty(sort))
            {
                dr = dt.Select(strWhere);  //strWhere条件筛选出需要的数据！
            }
            else
            {
                dr = dt.Select(strWhere, sort);
            }
            for (int i = 0; i < dr.Length; i++)
            {
                dtNew.Rows.Add(dr[i].ItemArray);  // 将DataRow添加到DataTable中
            }
            return dtNew;
        }

    }
}